package com.yc.sole.financial.controller;

import com.yc.sole.common.domain.StateCountResp;
import com.yc.sole.financial.biz.ReceiptBiz;
import com.yc.sole.financial.request.ReceiptBatchReq;
import com.yc.sole.financial.request.ReceiptQueryReq;
import com.yc.sole.financial.request.ReceiptReq;
import com.yc.sole.financial.response.ReceiptResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yizuomin
 * @date 2024-03-08
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class ReceiptController {

    private final ReceiptBiz receiptBiz;

    /**
     * 新增
     *
     * @param receiptReq
     * @return RestResult
     */
    @PreAuth(value = "financial:receipt:save", description = "新增")
    @PostMapping("/api/financial/receipt/saveReceipt")
    public Result<Void> saveReceipt(@RequestBody @Validated(SaveGroup.class) ReceiptReq receiptReq) {
        receiptBiz.saveReceipt(receiptReq);
        return Result.success();
    }

    /**
     * 新增
     *
     * @param batchReq
     * @return RestResult
     */
    @PreAuth(value = "financial:receipt:save", description = "新增")
    @PostMapping("/api/financial/receipt/saveBatchReceipt")
    public Result<Void> saveBatchReceipt(@RequestBody @Validated(SaveGroup.class) ReceiptBatchReq batchReq) {
        receiptBiz.saveBatchReceipt(batchReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param receiptReq
     * @return RestResult
     */
    @PreAuth(value = "financial:receipt:update", description = "修改")
    @PostMapping("/api/financial/receipt/updateReceiptById")
    public Result<Void> updateReceiptById(@RequestBody @Validated(UpdateGroup.class) ReceiptReq receiptReq) {
        receiptBiz.updateReceiptById(receiptReq);
        return Result.success();
    }

    /**
     * 通过id批量删除
     *
     * @param baseBatchIdsReq
     * @return RestResult
     */
    @PreAuth(value = "financial:receipt:delete", description = "删除")
    @PostMapping("/api/financial/receipt/deleteReceipt")
    public Result<Void> deleteReceipt(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        receiptBiz.deleteReceiptByIds(baseBatchIdsReq.getIds());
        return Result.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return Result
     */
    @PreAuth(value = "financial:receipt:select", description = "查询")
    @PostMapping("/api/financial/receipt/selectReceiptById/{id}")
    public Result<ReceiptResp> selectReceiptById(@PathVariable Long id) {
        return Result.data(receiptBiz.selectReceiptById(id));
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "financial:receipt:select", description = "查询")
    @PostMapping("/api/financial/receipt/selectReceiptPage")
    public Result<Paging<ReceiptResp>> selectReceiptPage(@RequestBody @Validated BasePageReq<ReceiptQueryReq> basePageReq) {
        return Result.data(receiptBiz.selectReceiptPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 查询收款方式的数量
     *
     * @param queryReq
     * @return Result
     */
    @PreAuth(value = "financial:receipt:select", description = "查询收款方式的数量")
    @PostMapping("/api/financial/receipt/selectReceiptMethodCount")
    public Result<List<StateCountResp>> selectReceiptMethodCount(@RequestBody ReceiptQueryReq queryReq) {
        return Result.data(receiptBiz.selectReceiptMethodCount(queryReq));
    }
}
